<?php

require_once( '../../libs/smarty.inc' );

require_once( '../../libs/Class_DB.php' );

require_once( '../../libs/Class_ERROR.php' );

require_once( '../../libs/login.inc.php' );

require_once( '../../libs/const.php' );



if( !in_array( "A", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;



$db = new Class_DB;



// 製品・単価を読込み

$db->query( "SELECT * FROM M_PRODUCTS WHERE category = 5 ORDER BY pro_name" );

$products[''] = '';

while( $row = $db->fetch() ){

	$products[ $row['id'] ] = $row['pro_name'];

	$costs[ $row['id'] ] = $row['cost'];

	$units[ $row['id'] ] = $row['unit'];

}





if( $_SERVER['REQUEST_METHOD'] == 'GET' ){

	

	$phase = 'input';

	

	if( isset($_GET['id']) && $_GET['id'] != '' ){



		// 更新

		if( is_numeric( $_GET['id'] ) ){

			

			$sql = sprintf("SELECT * FROM D_SHIPMENT WHERE id = %u", $_GET['id'] );

			$res = $db->query( $sql );

			$forms = mysql_fetch_array( $res, MYSQL_ASSOC );

			

			$sql = sprintf("SELECT * FROM D_SHIPMENT_DETAIL WHERE id = %u ORDER BY num", $_GET['id'] );

			$db->query( $sql );

			while( $row = $db->fetch() ){

				$forms['products'][$row['num']]	= $row['pro_cd'];

				$forms['denpyo'][$row['num']]	= $row['denpyo'];

				$forms['amount'][$row['num']]	= $row['amount'];

				$forms['from'][$row['num']]		= $row['sfrom'];

				$forms['biko'][$row['num']]		= $row['biko'];

				$forms['unit'][$row['num']]		= $row['unit'];

				$forms['cost'][$row['num']]		= $row['cost'];

			}

		}



		// コピーして新規作成

		if( isset( $_GET['mode'] ) && $_GET['mode'] == 'copy' ){

			$forms['id'] = '';

			foreach( $forms['products'] as $k => $v ){

				$forms['products'][$k]	= '';

				$forms['denpyo'][$k]	= '';

				$forms['amount'][$k]	= '';

				$forms['from'][$k]		= '';

				$forms['biko'][$k]		= '';

			}

		}



	}else{

		

		$forms['id'] = '';

		$forms['specify'] = 0;

		$forms['request_name'] = $login['staff_name'];

		

	}



}else{

	

	$forms = $_POST;

	

	// 入力画面から

	if( $forms['mode'] == 'input' ){

		$err = new Class_ERROR;

		

		if( $forms['id'] == '' ){

			$err_mes['request_name']	= $err->check( $forms['request_name'], array("EXIST") );



			for( $i=1; $i <= 10; $i++ ){

				$err_flg = false;

				$forms['products'][$i] = mb_convert_kana( $forms['products'][$i], "a");



				if( $forms['products'][$i] != '' ){

					

					if( $forms['amount'][$i] == '')	$err_flg = true;

					if( $forms['denpyo'][$i] == 0)	$err_flg = true;

					

					if( $err_flg ){

						$err_mes['detail'] = '<p class="error">No.' . $i . ' を入力してください</p>';

						$err->clear = false;

						break;

					}

				}

			}

		}

		

		if( $err->clear ){

			

			$forms['zipcode'] = mb_convert_kana( $forms['zipcode'], "a" );

			$forms['tel'] = mb_convert_kana( $forms['tel'], "a" );



			if( $forms['id'] == '' ){

				//新規登録

				$sql = sprintf( "INSERT INTO D_SHIPMENT(request_name, zipcode, address, tel, company, division, cust_name, limit_date, specify, memo, follow_text1, follow_text2, r_date, u_date) 

VALUES( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP )",

					$db->quote( $forms['request_name'] ),

					$db->quote( $forms['zipcode'] ),

					$db->quote( $forms['address'] ),

					$db->quote( $forms['tel'] ),

					$db->quote( $forms['company'] ),

					$db->quote( $forms['division'] ),

					$db->quote( $forms['cust_name'] ),

					$db->quote( $forms['limit_date'] ),

					$db->quote( $forms['specify'] ),

					$db->quote( $forms['memo'] )

				);

				$db->query( $sql );

				$forms['id'] = $db->last_id();



				// 詳細登録

				$search_text = "";

				for( $i=1; $i <= 10; $i++ ){

					if( $forms['products'][$i] != '' ){

						

						$sql = sprintf("INSERT INTO D_SHIPMENT_DETAIL(id, num, pro_cd, denpyo, amount, sfrom, biko, pro_name, cost, unit) 

VALUES( %u, %u, %u, %u, %u, '%s', '%s', '%s', %u, '%s' )",

							$forms['id'],

							$i,

							$forms['products'][$i],

							$forms['denpyo'][$i],

							$forms['amount'][$i],

							'',

							$db->quote( $forms['biko'][$i] ),

							$products[ $forms['products'][$i] ],

							$costs[$forms['products'][$i]],

							$units[$forms['products'][$i]]

						);

						$db->query( $sql );

						$search_text .= $products[ $forms['products'][$i] ] . ' ';

					}

				}



				// 検索用製品名登録

				$sql = sprintf( "UPDATE D_SHIPMENT SET search_text = '%s' WHERE id = %u", $db->quote( $search_text ), $forms['id'] );

				$db->query( $sql );



				$smarty->assign('preview_id', $forms['id'] );

				$forms['id'] = '';

				$message = '登録が完了しました。';



			}else{

				//更新

				$sql = sprintf( "UPDATE D_SHIPMENT SET 

zipcode = '%s', address = '%s', tel = '%s', company = '%s', division = '%s', cust_name = '%s', limit_date = '%s', specify = '%s', memo = '%s', 

follow_result = '%s', follow_date1 = '%s', follow_text1 = '%s', follow_date2 = '%s', follow_text2 = '%s', u_date = CURRENT_TIMESTAMP WHERE id = %u",

					$db->quote( $forms['zipcode'] ),

					$db->quote( $forms['address'] ),

					$db->quote( $forms['tel'] ),

					$db->quote( $forms['company'] ),

					$db->quote( $forms['division'] ),

					$db->quote( $forms['cust_name'] ),

					$db->quote( $forms['limit_date'] ),

					$db->quote( $forms['specify'] ),

					$db->quote( $forms['memo'] ),

					$db->quote( $forms['follow_result'] ),

					$db->quote( $forms['follow_date1'] ),

					$db->quote( $forms['follow_text1'] ),

					$db->quote( $forms['follow_date2'] ),

					$db->quote( $forms['follow_text2'] ),

					$forms['id']

				);

				$db->query( $sql );



				$message = '更新が完了しました。';

				$smarty->assign('preview_id', $forms['id'] );

			}

			

			$phase = 'complete';

		}else{

			$phase = 'input';

		}



	}



	// 削除

	if( $forms['mode'] == 'delete' ){



		$sql = sprintf("DELETE FROM D_SHIPMENT_DETAIL WHERE id = %u", $forms['id'] );

		$db->query( $sql );

		$sql = sprintf("DELETE FROM D_SHIPMENT WHERE id = %u", $forms['id'] );

		$db->query( $sql );



		$phase = 'complete';

		$message = '削除が完了しました。';

	}

	

}



if( isset( $forms['products'] ) && $phase != 'complete' ){

	foreach( $forms['products'] as $k => $v ){

		if( $forms['amount'][$k] != '' && is_numeric( $forms['amount'][$k] ) ){

			//$subtotal[$k] = $costs[ $v ] * $forms['amount'][$k];

			$subtotal[$k] = $forms['cost'][ $k ] * $forms['amount'][$k];

		}

	}

}



if( $phase == 'input' ){

	if( $forms['id'] == ''){

		$message = '内容を確認し、登録ボタンをクリックしてください。';

	}else{

		$message = '内容を確認し、更新ボタンをクリックしてください。';

	}

}



if( isset( $forms['id'] ) && $forms['id'] != '' ) $forms['disp_id'] = 'B' . sprintf( '%05d',$forms['id'] );

$smarty->assign('products', $products);

$smarty->assign('costs', $costs);

$smarty->assign('units', $units);

$smarty->assign('specify', array( 1=> 'あり', 0=> 'なし'));

$smarty->assign('message', $message);

$smarty->assign('phase', $phase);

$smarty->assign('category', $category );

$smarty->assign('arr_denpyo', $arr_denpyo );

$smarty->assign('arr_result', $arr_result );



if( isset( $subtotal ) )$smarty->assign('subtotal', $subtotal);

if( isset( $forms ) )	$smarty->assign('forms', $forms);

if( isset( $err_mes ) )	$smarty->assign('err', $err_mes);



$smarty->display('web-admin/ship_form.html');





?>